package com.siruier.boss.ui.activity.mall

import android.app.Activity
import android.view.View
import com.bigkoo.pickerview.view.OptionsPickerView
import com.siruier.boss.R
import com.siruier.boss.api.core.launchUI
import com.siruier.boss.api.core.toData
import com.siruier.boss.api.mallApi
import com.siruier.boss.bean.LocationSelect
import com.siruier.boss.bean.ShopAddressBean
import com.siruier.boss.databinding.ActivityAddOrEditreceivingAddressBinding
import com.siruier.boss.ui.api.ApiObserver
import com.siruier.boss.ui.base.BaseActivity
import com.siruier.boss.ui.bean.picker.BaseCityBean
import com.siruier.boss.ui.dialog.showAlertDialog
import com.siruier.boss.ui.ext.getString
import com.siruier.boss.ui.ext.objectClearNull
import com.siruier.boss.ui.ext.toInt
import com.siruier.boss.ui.ext.toastMessageLong
import com.siruier.boss.ui.helper.InjectBundle
import com.siruier.boss.ui.helper.PickerViewHelper
import com.siruier.boss.ui.utils.Utils
import com.siruier.boss.ui.utils.variableView

class ReceivingAddressAddOrEditActivity : BaseActivity<ActivityAddOrEditreceivingAddressBinding>() {
    private val mShopAddressBean: ShopAddressBean by InjectBundle { ShopAddressBean() }
    private var mCityOptionsPickerView: OptionsPickerView<BaseCityBean>? = null
    private var mProvBean: BaseCityBean? = null
    private var mCityBean: BaseCityBean? = null
    private var mCounBean: BaseCityBean? = null
    private val isEdit: Boolean by lazy { mShopAddressBean.id != null }

    override fun initView() {
        bindViewClick(vb.buttonSave, vb.etAddressArea, vb.titleView.rightTextView, vb.textLoc)
        if (isEdit) {
            vb.titleView.titleStr = R.string.title_edit_receiving_address.getString()
            vb.titleView.rightStr = R.string.button_delete.getString()
        }
        variableView {
            addTextView(vb.etConsignee)
            addTextView(vb.etPhoneNumber)
            addTextView(vb.etAddressArea)
            addTextView(vb.etDetailsAddress)
            addOnValidationResultListener(vb.buttonSave)
        }
    }

    override fun onClick(v: View?) {
        when (v) {
            vb.textLoc -> launchStartActivityForResult(LocationSelectActivity::class.java) {
                it.data?.getParcelableExtra<LocationSelect>("locationSelect")?.let { locationSelect ->
                    mProvBean = locationSelect.provBean
                    mCityBean = locationSelect.cityBean
                    mCounBean = locationSelect.counBean
                    vb.etAddressArea.text = "${mProvBean?.name}${mCityBean?.name}${mCounBean?.name}"
                    vb.etDetailsAddress.setText(locationSelect.poiName)
                }
            }
            vb.titleView.rightTextView -> {
                showAlertDialog {
                    message(R.string.dialog_address_delete)
                    positiveButton {
                        launchUI({ mallApi.addressDelete(mShopAddressBean.id!!).toData() }, ApiObserver(mThis) {
                            "删除成功".toastMessageLong()
                            setResult(Activity.RESULT_OK)
                            finish()
                        })
                    }
                    negativeButton { }
                }
            }
            vb.buttonSave -> {
                if (vb.etPhoneNumber.length() != 11) {
                    "手机号码格式不正确".toastMessageLong()
                    return
                }
                mShopAddressBean.run {
                    name = vb.etConsignee.text.toString()
                    phone = vb.etPhoneNumber.text.toString()
                    province = mProvBean!!.name
                    provinceCode = mProvBean!!.code
                    city = mCityBean!!.name
                    cityCode = mCityBean!!.code
                    county = mCounBean!!.name
                    countyCode = mCounBean!!.code
                    address = vb.etDetailsAddress.text.toString()
                    deful = vb.cbSettingDefault.isChecked.toInt.toString()
                }
                if (isEdit) {
                    launchUI({ mallApi.addressUpdate(mShopAddressBean.objectClearNull()).toData() }, ApiObserver(this) {
                        "添加成功".toastMessageLong()
                        setResult(RESULT_OK)
                        finish()
                    })
                } else {
                    launchUI({ mallApi.addressSave(mShopAddressBean.objectClearNull()).toData() }, ApiObserver(this) {
                        "添加成功".toastMessageLong()
                        setResult(Activity.RESULT_OK)
                        finish()
                    })
                }
            }
            vb.etAddressArea -> {
                Utils.hideSoftInput(this)
                launchUI({
                    if (mCityOptionsPickerView == null) {
                        PickerViewHelper.createProvCityAreaPickerView(mThis,
                            mOnOptionsSelectListener = { option1, option2, option3 ->
                                mProvBean = PickerViewHelper.getCityOptions1ByPosition(option1)
                                mCityBean = PickerViewHelper.getCityOptions2ByPosition(option1, option2)
                                mCounBean = PickerViewHelper.getCityOptions3ByPosition(option1, option2, option3)
                                vb.etAddressArea.text = "${mProvBean!!.name}${mCityBean!!.name}${mCounBean!!.name}"
                            }) {
                            mCityOptionsPickerView = it; mCityOptionsPickerView!!.show()
                        }
                    } else {
                        mCityOptionsPickerView!!.show()
                    }
                })
            }
        }
    }

    override fun initData() {
        onReload()
    }

    private fun onReload() {
        if (isEdit) {
            mShopAddressBean.run {
                vb.etConsignee.setText(name)
                vb.etPhoneNumber.setText(phone)
                vb.etDetailsAddress.setText(address)
                vb.etAddressArea.text = province + city + county
                vb.cbSettingDefault.isChecked = deful == "1"
                mProvBean = BaseCityBean().apply {
                    name = province
                    code = provinceCode
                }
                mCityBean = BaseCityBean().apply {
                    name = city
                    code = cityCode
                }
                mCounBean = BaseCityBean().apply {
                    name = county
                    code = countyCode
                }
            }
        }
    }
}